<?php

/**
 * This is the model class for table "{{purchase}}".
 *
 * The followings are the available columns in table '{{purchase}}':
 * @property integer $purchase_id
 * @property integer $wine_id
 * @property integer $invoice_id
 * @property integer $amount
 * @property double $tax
 *
 * The followings are the available model relations:
 * @property Invoice $invoice
 * @property Wine $wine
 */
class Purchase extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Purchase the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{purchase}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('wine_id, invoice_id, amount, tax', 'required'),
			array('wine_id, invoice_id, amount', 'numerical', 'integerOnly'=>true),
			array('tax', 'numerical'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('purchase_id, wine_id, invoice_id, amount, tax', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'invoice' => array(self::BELONGS_TO, 'Invoice', 'invoice_id'),
			'wine' => array(self::BELONGS_TO, 'Wine', 'wine_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'purchase_id' => 'ID',
			'wine_id' => 'Wein ID',
			'invoice_id' => 'Rechnungsnummer',
			'amount' => 'Menge',
			'tax' => 'MwSt',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('purchase_id',$this->purchase_id);
		$criteria->compare('wine_id',$this->wine_id);
		$criteria->compare('invoice_id',$this->invoice_id);
		$criteria->compare('amount',$this->amount);
		$criteria->compare('tax',$this->tax);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}